package com.ggh.jinjilive.views.live.model;

import android.app.Activity;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.Surface;

import com.blankj.utilcode.util.LogUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.common.Constants;
import com.ggh.jinjilive.mobile.live.im.CommonJson;
import com.ggh.jinjilive.mobile.live.im.PKHomeowners;
import com.ggh.jinjilive.utils.live.LiveMessageUtils;
import com.ggh.jinjilive.utils.loginIMUtils;
import com.ggh.jinjilive.views.live.pk.http.HttpRequests;
import com.ggh.jinjilive.views.live.pk.http.PKHttpRequests;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tencent.liteav.basic.log.TXCLog;
import com.tencent.rtmp.ITXLivePushListener;
import com.tencent.rtmp.TXLiveConstants;
import com.tencent.rtmp.TXLivePushConfig;
import com.tencent.rtmp.TXLivePusher;
import com.tencent.rtmp.ui.TXCloudVideoView;

public class CameraPushImpl implements CameraPush, ITXLivePushListener{

    private Context          mContext;
    private TXLivePusher     mLivePusher;
    private TXLivePushConfig mLivePushConfig;
    private TXCloudVideoView mPusherView;

    private OnLivePusherCallback mOnLivePusherCallback;

    private Bitmap mWaterMarkBitmap;

    private String mPusherURL = "";

    private boolean mIsPushing             = false;
    private boolean mIsResume              = false;
    private boolean mIsWaterMarkEnable     = true;
    private boolean mIsDebugInfo           = false;
    private boolean mIsMuteAudio           = false;
    private boolean mIsPrivateMode         = false;
    private boolean mIsLandscape           = true;
    private boolean mIsMirrorEnable        = false;
    private boolean mIsFocusEnable         = false;
    private boolean mIsZoomEnable          = false;
    private boolean mIsPureAudio           = false;
    private boolean mIsEarMonitoringEnable = false;
    private boolean mIsHWAcc               = false;
    private boolean mFrontCamera           = true;
    private boolean mIsEnableAdjustBitrate = false;
    private boolean mIsFlashLight          = false;

    private int mVideoResolution = TXLiveConstants.VIDEO_RESOLUTION_TYPE_720_1280;
    private int mAudioChannels = 2;
    private int mAudioSample;
    private int mQualityType = TXLiveConstants.VIDEO_QUALITY_HIGH_DEFINITION;


    /**
     * 默认美颜参数
     */
    private int mBeautyLevel    = 0;                                   // 美颜等级
    private int mBeautyStyle    = TXLiveConstants.BEAUTY_STYLE_SMOOTH; // 美颜样式
    private int mWhiteningLevel = 0;                                   // 美白等级
    private int mRuddyLevel     = 0;                                   // 红润等级

    private boolean isCustom = false;
    private long mTimeDiff = 0;        //客户端和服务器时间差，用户连麦和PK请求超时处理

    private TXPhoneStateListener mPhoneListener;
    private ActivityRotationObserver mActivityRotationObserver;



    //获取网络请求
    public PKHttpRequests pkHttpRequests;
    public PKHttpRequests getPKHttpRequests(){
       return pkHttpRequests;
    }

    public CameraPushImpl(Context context, TXCloudVideoView pusherView) {
        initialize(context, pusherView);
    }

    public CameraPushImpl(Context context, boolean isCustom) {
        this.isCustom = isCustom;
        initialize(context);
    }

    private void initialize(Context context, TXCloudVideoView pusherView) {
        pkHttpRequests = new PKHttpRequests();

        mContext = context;
        mLivePusher = new TXLivePusher(context);
        mPusherView = pusherView;
        mLivePushConfig = new TXLivePushConfig();
        mLivePushConfig.setVideoEncodeGop(5);
        mLivePusher.setConfig(mLivePushConfig);
        mWaterMarkBitmap = decodeResource(mContext.getResources(), R.mipmap.ic_launcher_round);
        initListener();
    }

    private void initialize(Context context) {
        mContext = context;
        mLivePusher = new TXLivePusher(context);
        mLivePushConfig = new TXLivePushConfig();
        mLivePushConfig.setVideoEncodeGop(5);
        mLivePusher.setConfig(mLivePushConfig);
        mWaterMarkBitmap = decodeResource(mContext.getResources(), R.mipmap.ic_launcher_round);
        initListener();
    }

    /**
     * 初始化电话监听、系统是否打开旋转监听
     */
    private void initListener() {
        mPhoneListener = new TXPhoneStateListener();
        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Service.TELEPHONY_SERVICE);
        tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
        mActivityRotationObserver = new ActivityRotationObserver(new Handler(Looper.getMainLooper()));
        mActivityRotationObserver.startObserver();

    }

    /**
     * 销毁
     */
    private void unInitPhoneListener() {
        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Service.TELEPHONY_SERVICE);
        tm.listen(mPhoneListener, PhoneStateListener.LISTEN_NONE);
        mActivityRotationObserver.stopObserver();
    }


    /**
     * 电话监听
     */
    private class TXPhoneStateListener extends PhoneStateListener {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:   //电话等待接听
                case TelephonyManager.CALL_STATE_OFFHOOK:   //电话接听
                    pause();
                    break;
                case TelephonyManager.CALL_STATE_IDLE:      //电话挂机
                    resume();
                    break;
            }
        }
    }

    /**
     * 观察屏幕旋转设置变化
     */
    private class ActivityRotationObserver extends ContentObserver {
        ContentResolver mResolver;

        public ActivityRotationObserver(Handler handler) {
            super(handler);
            mResolver = mContext.getContentResolver();
        }

        //屏幕旋转设置改变时调用
        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            if (isActivityCanRotation(mContext)) {
                setRotationForActivity();
            } else {
                // 恢复到正方向
                mLivePushConfig.setHomeOrientation(TXLiveConstants.VIDEO_ANGLE_HOME_DOWN);
                // 恢复渲染角度
                mLivePusher.setRenderRotation(0);
                if (mLivePusher.isPushing()) {
                    mLivePusher.setConfig(mLivePushConfig);
                }
            }
            if (mOnLivePusherCallback != null) {
                mOnLivePusherCallback.onActivityRotationObserverChange(selfChange);
            }
        }

        public void startObserver() {
            mResolver.registerContentObserver(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), false, this);
        }

        public void stopObserver() {
            mResolver.unregisterContentObserver(this);
        }
    }

    /**
     * 判断系统 "自动旋转" 设置功能是否打开
     *
     * @return false---Activity可根据重力感应自动旋转
     */
    private boolean isActivityCanRotation(Context context) {
        int flag = Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
        return flag != 0;
    }



    /**
     * 获取资源图片
     *
     * @param resources
     * @param id
     * @return
     */
    private Bitmap decodeResource(Resources resources, int id) {
        TypedValue value = new TypedValue();
        resources.openRawResource(id, value);
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inTargetDensity = value.density;
        return BitmapFactory.decodeResource(resources, id, opts);
    }

    @Override
    public void setURL(String pushURL) {
        mPusherURL = pushURL;

    }

    @Override
    public void startPush() {
        int resultCode = Constants.PLAY_STATUS_SUCCESS;
        String tRTMPURL = "";
        if (!TextUtils.isEmpty(mPusherURL)) {
            String url[] = mPusherURL.split("###");
            if (url.length > 0) {
                tRTMPURL = url[0];
            }
        }
        if (TextUtils.isEmpty(tRTMPURL) || (!tRTMPURL.trim().toLowerCase().startsWith("rtmp://"))) {
            resultCode = Constants.PLAY_STATUS_INVALID_URL;
        } else {
            // 添加播放回调
            mLivePusher.setPushListener(this);
            // 添加后台垫片推流参数
            Bitmap bitmap = decodeResource(mContext.getResources(), R.mipmap.livepusher_pause_publish);
            mLivePushConfig.setPauseImg(bitmap);
            mLivePushConfig.setPauseImg(300, 5);
            mLivePushConfig.setPauseFlag(TXLiveConstants.PAUSE_FLAG_PAUSE_VIDEO);// 设置暂停时，只停止画面采集，不停止声音采集。
            // 是否打开曝光对焦
            mLivePushConfig.setTouchFocus(mIsFocusEnable);
            // 是否打开手势放大预览画面
            mLivePushConfig.setEnableZoom(mIsZoomEnable);
            //开启纯音频推流。
            mLivePushConfig.enablePureAudioPush(mIsPureAudio);
            //开启音频
            mLivePushConfig.enableAudioEarMonitoring(mIsEarMonitoringEnable);
            // 设置推流配置
            mLivePusher.setConfig(mLivePushConfig);
            // 设置场景
            setPushScene(mQualityType, mIsEnableAdjustBitrate);
            // 设置声道数。
            mLivePushConfig.setAudioChannels(mAudioChannels);
            // 设置音频采样率
            mLivePushConfig.setAudioSampleRate(mAudioSample);
            // 设置 TXLivePusher 推流配置项。
            mLivePusher.setConfig(mLivePushConfig);
            //是否开启 MainProfile 硬编码模式。
            mLivePushConfig.enableVideoHardEncoderMainProfile(true);
            //设置回声消除。
            mLivePushConfig.enableAEC(true);
            // 设置本地预览View
            if (mPusherView != null) {
                mLivePusher.startCameraPreview(mPusherView);
            }
            if (!mFrontCamera){
                mLivePusher.switchCamera();
            }

            LogUtils.d(tRTMPURL.trim());
            // 发起推流
            resultCode = mLivePusher.startPusher(tRTMPURL.trim());
        }
        LiveMessageUtils.onPushStart(resultCode);
    }

    @Override
    public void stopPush() {
        if(null != mLivePusher){
            // 停止本地预览
            mLivePusher.stopCameraPreview(true);
            // 移除监听
            mLivePusher.setPushListener(null);
            // 停止推流
            mLivePusher.stopPusher();

            if (mOnLivePusherCallback != null) {
                mOnLivePusherCallback.onPushStop();
            }
        }
    }

    @Override
    public void togglePush() {
        if (mIsPushing) {
            stopPush();
        } else {
            startPush();
        }
    }

    @Override
    public void resume() {
        if (mPusherView != null) {
            mPusherView.onResume();

        }
        if (mLivePusher != null) {
            mLivePusher.resumePusher();
        }
        if (mOnLivePusherCallback != null) {
            mOnLivePusherCallback.onPushResume();
        }
    }

    @Override
    public void resumePusher() {
        mLivePusher.resumePusher();
    }

    @Override
    public void pause() {
        LogUtils.d("直播停止推流");
        if (mPusherView != null) {
            //暂停摄像头
            mPusherView.onPause();
        }
        if(null != mLivePusher){
            //暂停摄像头或屏幕采集并进入垫片推流状态。
            mLivePusher.pausePusher();

        }
    }

    @Override
    public void pausePusher() {
        if(null != mLivePusher){
            //暂停摄像头或屏幕采集并进入垫片推流状态。
            mLivePusher.pausePusher();
        }

    }

    @Override
    public void destroy() {
        stopPush();
        if (mPusherView != null) {
            mPusherView.onDestroy();
        }
        unInitPhoneListener();
    }

    @Override
    public void switchCamera() {
        //切换摄像头
        if(null != mLivePusher){
            mFrontCamera = !mFrontCamera;
            mLivePusher.switchCamera();
        }
    }

    @Override
    public void setRotationForActivity() {
// 自动旋转打开，Activity随手机方向旋转之后，需要改变推流方向
        int mobileRotation = ((Activity) mContext).getWindowManager().getDefaultDisplay().getRotation();
        int pushRotation = TXLiveConstants.VIDEO_ANGLE_HOME_DOWN;
        switch (mobileRotation) {
            case Surface.ROTATION_0:
                pushRotation = TXLiveConstants.VIDEO_ANGLE_HOME_DOWN;
                break;
            case Surface.ROTATION_180:
                pushRotation = TXLiveConstants.VIDEO_ANGLE_HOME_UP;
                break;
            case Surface.ROTATION_90:
                pushRotation = TXLiveConstants.VIDEO_ANGLE_HOME_RIGHT;
                break;
            case Surface.ROTATION_270:
                pushRotation = TXLiveConstants.VIDEO_ANGLE_HOME_LEFT;
                break;
            default:
                break;
        }

        mLivePusher.setRenderRotation(0);                                   // 因为activity也旋转了，本地渲染相对正方向的角度为0。
        mLivePushConfig.setHomeOrientation(pushRotation);                   // 根据Activity方向，设置采集角度

        if (mLivePusher.isPushing()) {                                      // 当前正在推流，
            mLivePusher.setConfig(mLivePushConfig);
            if (!mIsPrivateMode) {                       // 不是隐私模式，则开启摄像头推流。
                mLivePusher.stopCameraPreview(true);
                if (mPusherView != null){
                    mLivePusher.startCameraPreview(mPusherView);
                }
            }
        }
    }

    @Override
    public void setOnLivePusherCallback(OnLivePusherCallback callback) {
        mOnLivePusherCallback = callback;
    }

    @Override
    public TXLivePusher getTXLivePusher() {
        return mLivePusher;
    }

    @Override
    public boolean isPushing() {
        return mIsPushing;
    }

    @Override
    public void setHomeOrientation(boolean isPortrait) {
        mIsLandscape = isPortrait;
        int renderRotation;
        if (isPortrait) {  // 横屏
            mLivePushConfig.setHomeOrientation(TXLiveConstants.VIDEO_ANGLE_HOME_RIGHT);
            renderRotation = 90; // 因为采集旋转了，那么保证本地渲染是正的，则设置渲染角度为90度。
        } else {            // 竖屏
            mLivePushConfig.setHomeOrientation(TXLiveConstants.VIDEO_ANGLE_HOME_DOWN);
            renderRotation = 0;
        }
        if (mLivePusher.isPushing()) {
            mLivePusher.setConfig(mLivePushConfig);
            mLivePusher.setRenderRotation(renderRotation);
        }
    }

    @Override
    public void setPrivateMode(boolean enable) {
        mIsPrivateMode = enable;
        // 隐私模式下，会进入垫片推流
        if (mIsPushing) {
            if (enable) {
                mLivePusher.pausePusher();
            } else {
                mLivePusher.resumePusher();
            }
        }
    }

    @Override
    public void setMute(boolean enable) {
        mIsMuteAudio = enable;
        mLivePusher.setMute(enable);
    }

    @Override
    public void setMirror(boolean enable) {
        mIsMirrorEnable = enable;
        mLivePusher.setMirror(enable);
    }

    @Override
    public void setVideoEncoderXMirror(boolean enable) {
        //设置观众端水平镜像。
        mLivePushConfig.setVideoEncoderXMirror(enable);
        mLivePusher.setConfig(mLivePushConfig);
    }

    @Override
    public void setLocalVideoMirrorType(int mirrorType) {
        mLivePushConfig.setLocalVideoMirrorType(mirrorType);
        mLivePusher.setConfig(mLivePushConfig);
    }

    @Override
    public void turnOnFlashLight(boolean enable) {
        mIsFlashLight = enable;
        mLivePusher.turnOnFlashLight(enable);
    }

    @Override
    public void showLog(boolean enable) {
        mIsDebugInfo = enable;
        if (mPusherView != null) {
            mPusherView.showLog(enable);
        }
    }

    @Override
    public void setWatermark(boolean enable) {

    }

    @Override
    public void setTouchFocus(boolean enable) {
        mIsFocusEnable = enable;
        mLivePushConfig.setTouchFocus(enable);
        if (mLivePusher.isPushing()) {
            stopPush();
            startPush();
        }
    }

    @Override
    public void setEnableZoom(boolean enable) {
        mIsZoomEnable = enable;
        mLivePushConfig.setEnableZoom(enable);
        if (mLivePusher.isPushing()) {
            stopPush();
            startPush();
        }
    }

    @Override
    public void snapshot() {
        mLivePusher.snapshot(new TXLivePusher.ITXSnapshotListener() {
            @Override
            public void onSnapshot(Bitmap bitmap) {
               // saveSnapshotBitmap(bitmap);
            }
        });
    }

    @Override
    public void sendMessage(String msg) {
        mLivePusher.sendMessage(msg.getBytes());
    }

    @Override
    public void setHardwareAcceleration(boolean enable) {
        mIsHWAcc = enable;
        if (enable) {
            mLivePushConfig.setHardwareAcceleration(TXLiveConstants.ENCODE_VIDEO_HARDWARE); // 启动硬编
        } else {
            mLivePushConfig.setHardwareAcceleration(TXLiveConstants.ENCODE_VIDEO_SOFTWARE); // 启动软编
        }
        if (mLivePusher.isPushing()) {
            // 硬件加速变更不需要重启推流，直接应用配置项即可
            mLivePusher.setConfig(mLivePushConfig);
        }
    }

    @Override
    public void setAdjustBitrate(boolean enable, int qualityType) {
        mIsEnableAdjustBitrate = enable;
        setPushScene(qualityType, enable);
    }

    @Override
    public void setQuality(boolean enable, int type) {
        setPushScene(type, enable);
    }

    @Override
    public void enableAudioEarMonitoring(boolean enable) {
        mIsEarMonitoringEnable = enable;
        if (mLivePusher != null) {
            TXLivePushConfig config = mLivePusher.getConfig();
            config.enableAudioEarMonitoring(enable);
            mLivePusher.setConfig(config);
        }
    }

    @Override
    public void enablePureAudioPush(boolean enable) {
        mIsPureAudio = enable;
    }

    /**
     * 响应跨房 PK 请求
     * @param pkHomeowners
     * @return
     */
    @Override
    public int responseRoomPK(PKHomeowners pkHomeowners) {
        CommonJson<PKHomeowners> response = new CommonJson<>();
        response.cmd = "pk";
        response.data = pkHomeowners;
        String content = new Gson().toJson(response, new TypeToken<CommonJson<PKHomeowners>>() {}.getType());
        //发送CC（端到端）自定义消息
        LogUtils.d("响应跨房 PK 请求" + new Gson().toJson(content));
        loginIMUtils.sendC2CCustomMessage(content.getBytes(),pkHomeowners.getUserID());
        return 0;
    }

    @Override
    public void requestRoomPK(PKHomeowners pkHomeowners) {

        CommonJson<PKHomeowners> response = new CommonJson<>();
        response.cmd = "pk";
        response.data = pkHomeowners;
        String content = new Gson().toJson(response, new TypeToken<CommonJson<PKHomeowners>>() {}.getType());
        LogUtils.d("发送CC" + content);
        //发送CC（端到端）自定义消息
        loginIMUtils.sendC2CCustomMessage(content.getBytes(),pkHomeowners.getOn_user_id());
    }

    @Override
    public void setAudioQuality(int channel, int sampleRate) {
        mAudioChannels = channel;
        mAudioSample = sampleRate;
        if (mLivePusher != null) {
            TXLivePushConfig config = mLivePusher.getConfig();
            config.setAudioChannels(channel);
            config.setAudioSampleRate(sampleRate);
            mLivePusher.setConfig(config);
        }
    }
    //推流事件通知。
    @Override
    public void onPushEvent(int event, Bundle bundle) {
        String msg = bundle.getString(TXLiveConstants.EVT_DESCRIPTION);
        String pushEventLog = event + ", " + msg;
        // 如果开始推流，设置了隐私模式。 需要在回调里面设置，不能直接start之后直接pause
        if (event == TXLiveConstants.PUSH_EVT_PUSH_BEGIN) {
            if (mIsPrivateMode) {
                pausePusher();
            }
        }
        if (event == TXLiveConstants.PUSH_ERR_NET_DISCONNECT
                || event == TXLiveConstants.PUSH_ERR_INVALID_ADDRESS
                || event == TXLiveConstants.PUSH_ERR_OPEN_CAMERA_FAIL
                || event == TXLiveConstants.PUSH_ERR_OPEN_MIC_FAIL) {
            // 遇到以上错误，则停止推流
            stopPush();
        } else if (event == TXLiveConstants.PUSH_WARNING_HW_ACCELERATION_FAIL) {
            // 开启硬件加速失败
            mLivePushConfig.setHardwareAcceleration(TXLiveConstants.ENCODE_VIDEO_SOFTWARE);
            mLivePusher.setConfig(mLivePushConfig);
        } else if (event == TXLiveConstants.PUSH_EVT_CHANGE_RESOLUTION) {
            LogUtils.d("change resolution to " + bundle.getInt(TXLiveConstants.EVT_PARAM2) + ", bitrate to" + bundle.getInt(TXLiveConstants.EVT_PARAM1));
        } else if (event == TXLiveConstants.PUSH_EVT_CHANGE_BITRATE) {
            LogUtils.d("change bitrate to" + bundle.getInt(TXLiveConstants.EVT_PARAM1));
        } else if (event == TXLiveConstants.PUSH_EVT_OPEN_CAMERA_SUCC) {
            // 只有后置摄像头可以打开闪光灯，若默认需要开启闪光灯。 那么在打开摄像头成功后，才可以进行配置。 若果当前是前置，设定无效；若是后置，打开闪光灯。
            turnOnFlashLight(mIsFlashLight);
        }
        if (mOnLivePusherCallback != null) {
            mOnLivePusherCallback.onPushEvent(event, bundle);
        }


    }
    //网络状态通知。
    @Override
    public void onNetStatus(Bundle bundle) {
//        LogUtils.d("自己, CPU:" + bundle.getString(TXLiveConstants.NET_STATUS_CPU_USAGE) +
//                ", RES:" + bundle.getInt(TXLiveConstants.NET_STATUS_VIDEO_WIDTH) + "*" + bundle.getInt(TXLiveConstants.NET_STATUS_VIDEO_HEIGHT) +
//                ", SPD:" + bundle.getInt(TXLiveConstants.NET_STATUS_NET_SPEED) + "Kbps" +
//                ", FPS:" + bundle.getInt(TXLiveConstants.NET_STATUS_VIDEO_FPS) +
//                ", ARA:" + bundle.getInt(TXLiveConstants.NET_STATUS_AUDIO_BITRATE) + "Kbps" +
//                ", VRA:" + bundle.getInt(TXLiveConstants.NET_STATUS_VIDEO_BITRATE) + "Kbps");
    }



    /**
     * 设置推流场景
     * <p>
     * SDK 内部将根据具体场景，进行推流 分辨率、码率、FPS、是否启动硬件加速、是否启动回声消除 等进行配置
     * <p>
     * 适用于一般客户，方便快速进行配置
     * <p>
     * 专业客户，推荐通过 {@link TXLivePushConfig} 进行逐一配置
     */
    private void setPushScene(int type, boolean enableAdjustBitrate) {
        mQualityType = type;
        mIsEnableAdjustBitrate = enableAdjustBitrate;
        // 码率、分辨率自适应都关闭
        boolean autoResolution = false;
        switch (type) {
            case TXLiveConstants.VIDEO_QUALITY_STANDARD_DEFINITION:     /*360p*/
                if (mLivePusher != null) {
                    mLivePusher.setVideoQuality(TXLiveConstants.VIDEO_QUALITY_STANDARD_DEFINITION, enableAdjustBitrate, autoResolution);
                    mVideoResolution = TXLiveConstants.VIDEO_RESOLUTION_TYPE_360_640;
                }
                break;
            case TXLiveConstants.VIDEO_QUALITY_HIGH_DEFINITION:         /*540p*/
                if (mLivePusher != null) {
                    mLivePusher.setVideoQuality(TXLiveConstants.VIDEO_QUALITY_HIGH_DEFINITION, enableAdjustBitrate, autoResolution);
                    mVideoResolution = TXLiveConstants.VIDEO_RESOLUTION_TYPE_540_960;
                }
                break;
            case TXLiveConstants.VIDEO_QUALITY_SUPER_DEFINITION:        /*720p*/
                if (mLivePusher != null) {
                    mLivePusher.setVideoQuality(TXLiveConstants.VIDEO_QUALITY_SUPER_DEFINITION, enableAdjustBitrate, autoResolution);
                    mVideoResolution = TXLiveConstants.VIDEO_RESOLUTION_TYPE_720_1280;
                }
                break;
            case TXLiveConstants.VIDEO_QUALITY_ULTRA_DEFINITION:        /*1080p*/
                if (mLivePusher != null) {
                    mLivePusher.setVideoQuality(TXLiveConstants.VIDEO_QUALITY_ULTRA_DEFINITION, enableAdjustBitrate, autoResolution);
                    mVideoResolution = TXLiveConstants.VIDEO_RESOLUTION_TYPE_1080_1920;
                }
                break;
            case TXLiveConstants.VIDEO_QUALITY_LINKMIC_MAIN_PUBLISHER:  /*连麦大主播*/
                if (mLivePusher != null) {
                    mLivePusher.setVideoQuality(TXLiveConstants.VIDEO_QUALITY_LINKMIC_MAIN_PUBLISHER, enableAdjustBitrate, autoResolution);
                    mVideoResolution = TXLiveConstants.VIDEO_RESOLUTION_TYPE_540_960;
                }
                break;
            case TXLiveConstants.VIDEO_QUALITY_LINKMIC_SUB_PUBLISHER:   /*连麦小主播*/
                if (mLivePusher != null) {
                    mLivePusher.setVideoQuality(TXLiveConstants.VIDEO_QUALITY_LINKMIC_SUB_PUBLISHER, enableAdjustBitrate, autoResolution);
                    mVideoResolution = TXLiveConstants.VIDEO_RESOLUTION_TYPE_320_480;
                }
                break;
            case TXLiveConstants.VIDEO_QUALITY_REALTIEM_VIDEOCHAT:      /*实时*/
                if (mLivePusher != null) {
                    mLivePusher.setVideoQuality(TXLiveConstants.VIDEO_QUALITY_REALTIEM_VIDEOCHAT, enableAdjustBitrate, autoResolution);
                    mVideoResolution = TXLiveConstants.VIDEO_RESOLUTION_TYPE_360_640;
                }
                break;
            default:
                break;
        }
        // 设置场景化配置后，SDK 内部会根据场景自动选择相关的配置参数，所以我们这里把内部的config获取出来，赋值到外部。
        mLivePushConfig = mLivePusher.getConfig();

        // 是否开启硬件加速
        if (mIsHWAcc) {
            mLivePushConfig.setHardwareAcceleration(TXLiveConstants.ENCODE_VIDEO_HARDWARE);
            mLivePusher.setConfig(mLivePushConfig);
        }
    }


}
